const isPalindrome = (head) => {
  if (head == null || head.next == null) return true;
  let fast = head;
  let slow = head;
  let prev;
  // slow 的两种情况：slow 正好指向中间两个结点的后一个; slow 正好指向中间结点
  while (fast && fast.next) {
    prev = slow;
    slow = slow.next;
    fast = fast.next.next;
  }
  prev.next = null; // 断成两个链表
  // 翻转后半段
  let head2 = null;
  while (slow) {
    const temp = slow.next;
    slow.next = head2;
    head2 = slow;
    slow = temp;
  }
  // 比对
  while (head && head2) {
    if (head.val !== head2.val) {
      return false;
    }
    head = head.next;
    head2 = head2.next;
  }
  return true;
};
